package com.tong.embeddedvr.service.impl;

import com.tong.embeddedvr.Mapper.HostMapper;
import com.tong.embeddedvr.dto.HostDTO;
import com.tong.embeddedvr.entity.HostEntity;
import com.tong.embeddedvr.service.HostService;
import com.tong.embeddedvr.utils.CommonUtil;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class HostServiceImpl implements HostService {

    @Autowired
    HostMapper hostMapper;

    private final Logger logger = LoggerFactory.getLogger(HostServiceImpl.class);

    @Override
    public String addHost(HostDTO hostDTO){
        String res;
        DefaultMapperFactory mapperFactory = CommonUtil.getMapperFactory();
        HostEntity prop = mapperFactory.getMapperFacade().map(hostDTO, HostEntity.class);
        try{
            hostMapper.addHost(prop);
            res = CommonUtil.ajaxReturn("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("主机添加失败|主机信息:[{}]",prop);
            res = CommonUtil.ajaxReturn(400,"更新失败");
        }
        return res;
    }

    @Override
    public String updateInformation(HostDTO hostDTO) {
        String res;
        DefaultMapperFactory mapperFactory = CommonUtil.getMapperFactory();
        HostEntity prop = mapperFactory.getMapperFacade().map(hostDTO, HostEntity.class);
        try{
            hostMapper.updateInformation(prop);
            res = CommonUtil.ajaxReturn("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("主机信息更新失败|更新信息:[{}]",prop);
            res = CommonUtil.ajaxReturn(400,"更新失败");
        }
        return res;
    }

    @Override
    public String getAllHost() {
        String res;
        try{
            HostDTO[] data = hostMapper.getAllHost();
            res = CommonUtil.ajaxReturn(data);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取所有主机信息失败");
            res = CommonUtil.ajaxReturn(400,"更新失败");
        }
        return res;
    }

    public HostDTO getLessUseHost() {
        HostDTO[] allHost = hostMapper.getAllHost();
        for (HostDTO hostEntity : allHost) {
            System.out.println("host:" + hostEntity);
        }
        HostDTO ans = null;
        int less = 8,temp ,len = allHost.length;
        for(int i=0;i < len;i++){
            temp = allHost[i].getHostUsernum();
            if(temp < less){
                less = temp;
                ans = allHost[i];
            }
        }
        return ans;
    }

    @Override
    public void increaseOneUser(HostDTO hostDTO) {
        DefaultMapperFactory mapperFactory = CommonUtil.getMapperFactory();
        HostEntity prop = mapperFactory.getMapperFacade().map(hostDTO, HostEntity.class);
        hostMapper.increaseUseNum(prop);
    }

    @Override
    public void decreaseOneUser(HostDTO hostDTO) {
        DefaultMapperFactory mapperFactory = CommonUtil.getMapperFactory();
        HostEntity prop = mapperFactory.getMapperFacade().map(hostDTO, HostEntity.class);
        hostMapper.decreaseUseNum(prop);
    }
}
